package cn.openea.perpartner.cms.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.cms.web.dto.CustomerLogDto;
import cn.openea.perpartner.cms.web.dto.SupplementCheck;
import cn.openea.perpartner.cms.web.entity.CmsSupplement;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.openea.perpartner.cms.factory.UserContext;
import cn.openea.perpartner.cms.web.dto.SupplementDto;
import cn.openea.perpartner.cms.web.entity.CmsCustomerLog;
import cn.openea.perpartner.cms.web.mapper.CmsCustomerLogMapper;
import cn.openea.perpartner.cms.web.mapper.CmsSupplementMapper;
import cn.openea.perpartner.cms.web.service.ICmsSupplementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * (CmsSupplement)表服务实现类
 *
 * @author makejava
 * @since 2022-08-23 12:10:32
 */
@Service("cmsSupplementService")
public class CmsSupplementServiceImpl extends ServiceImpl<CmsSupplementMapper, CmsSupplement> implements ICmsSupplementService {
    @Autowired
    private CmsCustomerLogMapper customerLogMapper;
    @Override
    public CmsSupplement getByCustomerId(String customerId) {
        LambdaQueryWrapper<CmsSupplement> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CmsSupplement::getCustomerId,customerId);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public boolean check(String customerId) {
        CmsSupplement supplement = getByCustomerId(customerId);
        if(BeanUtil.isEmpty(supplement)){
            return false;
        }
        SupplementCheck supplementCheck = new SupplementCheck();
        BeanUtil.copyProperties(supplement,supplementCheck);
        Map<String, Object> supplementMap = BeanUtil.beanToMap(supplementCheck);
        for(Map.Entry<String, Object> entry : supplementMap.entrySet()){
            if(ObjectUtil.isEmpty(entry.getValue())){
                return false;
            }
        }
        return true;
    }
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public boolean update(SupplementDto supplementDto) {
        //将前端customerDto数据对象转为为customer对象，取客户基础数据
        boolean isSuccess;
        CmsSupplement supplement = supplementDto.getSupplement();
        if (StrUtil.isBlank(supplement.getSupplementId())){
            supplement.setCreateUser(UserContext.getUserId());
        }
        supplement.setUpdateUser(UserContext.getUserId());
        //更新商品辅表数据
        isSuccess = saveOrUpdate(supplement);
        //从SupplementDto对象取任务跟踪记录数据
        List<CustomerLogDto> customerLogDtoList = supplementDto.getLogData();
        List<Long> logIdList = new ArrayList<>();
        for (CustomerLogDto customerLogDto : customerLogDtoList) {
            logIdList.add(customerLogDto.getCustomerLogId());
        }
        LambdaQueryWrapper<CmsCustomerLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(CmsCustomerLog::getUnid, logIdList);
        wrapper.eq(CmsCustomerLog::getUserid, UserContext.getUserId());
        List<CmsCustomerLog> customerLogList = customerLogMapper.selectList(wrapper);
        //insert,第一次跟踪记录
        long customerId = Long.parseLong(supplement.getCustomerId());
        int len = customerLogDtoList.size() - 1;
        CustomerLogDto customerLogDto = customerLogDtoList.get(len);
        if(customerLogList.isEmpty()){
            CmsCustomerLog customerLog = new CmsCustomerLog();
            customerLog.setUserid(UserContext.getUserId());
            customerLog.setCustomerId(customerId);
            customerLog.setContent(customerLogDto.getContent());
            return customerLogMapper.insert(customerLog) > 0;
        }
        //insert n+1次跟踪记录
        if(logIdList.size()>customerLogList.size()){
            CmsCustomerLog customerLog = new CmsCustomerLog();
            customerLog.setUserid(UserContext.getUserId());
            customerLog.setCustomerId(customerId);
            customerLog.setContent(customerLogDto.getContent());
            isSuccess =  customerLogMapper.insert(customerLog) > 0;
        }
        //update
        for (int i = 0; i < customerLogList.size(); i++) {
            customerLogDto = customerLogDtoList.get(i);
            //跟踪记录不变，则不进行数据更新
            if (customerLogList.get(i).getContent().equals(customerLogDto.getContent())) {
                continue;
            }
            customerLogList.get(i).setContent(customerLogDto.getContent());
            isSuccess = customerLogMapper.updateById(customerLogList.get(i)) > 0;
        }
        return isSuccess;
    }
}

